﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Text.RegularExpressions;
using System.Xml;
using CMSModel;

/// <summary>
///NewspapersCatch 的摘要说明
/// </summary>
public class NewspapersCatch
{
    string urlReg = "<li><a href=\"/MS[0-9]{1,30}.shtml\">[\\s\\S]{1,30}</a></li>";
    string sourceReg = "<div id=\"con1\" class=\"con1\">([\\s\\S]*?)</div>";
    string startReg = "<div id=\"NewsContentText\" class=\"con_editor\" style=\"overflow-x: hidden;\">[\\s\\S]*<div class=\"con5\">\\s{1,100}（证券之星编辑整理）\\s{0,100}</div>";

    //<div class="Line"></div> 
    public void Import()
    {
        WebClient client = new WebClient();
        string str = client.DownloadString("http://finance.stockstar.com/finance/mediacenter.htm");
        MatchCollection mc = Regex.Matches(str, urlReg, RegexOptions.None);
        for (int i = 0; i < mc.Count; i++)
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                string tt = mc[i].Value;
                doc.LoadXml(tt);
                var root = doc.DocumentElement;
                var title = root.ChildNodes[0].InnerText;
                var url = "http://finance.stockstar.com" + root.ChildNodes[0].Attributes["href"].Value;

                if (IsCheck(url))
                {
                    continue;
                }
                WebClient client2 = new WebClient();
                client2.Dispose();
                string str2 = client2.DownloadString(url);
                Match sourceMatch = Regex.Match(str2, sourceReg);
                string source = string.Empty;
                string author = "";
                if (sourceMatch.Success)
                {
                    XmlDocument tDoc = new XmlDocument();
                    string temp = sourceMatch.Value.Replace("&", "&amp;");
                    tDoc.LoadXml(temp);
                    var alst = tDoc.DocumentElement.SelectNodes("a");
                    if (alst.Count == 2)
                    {
                        author = alst[0].InnerText;
                        source = alst[1].InnerText;
                    }
                    else if (alst.Count == 1)
                    {
                        source = alst[0].InnerText;
                    }
                }
                Match contentMatch = Regex.Match(str2, startReg);
                string content = string.Empty;
                if (contentMatch.Success)
                {
                    content = contentMatch.Value;
                    content = Regex.Replace(content, "<div[^>]*>", "");
                    content = Regex.Replace(content, "</div>", "");
                    content = content.Replace("<!--来源、关键字微博-->", "");
                    content = Regex.Replace(content, @"<a[^>]*>", "");
                    content = Regex.Replace(content, "</a>", "");
                    content = Regex.Replace(content, @"<img[^>]*>", "");
                    content = content.Replace("（证券之星编辑整理）", "");
                    content = content.Replace("证券之星", "");
                    content = content.Replace("（<U>爱基</U>,<U>净值</U>,<U>资讯</U>）", "");
                    content = Regex.Replace(content, "<span[^>]*>", "");
                    content = Regex.Replace(content, "</span>", "");
                    content = NewsImportBLL.ReplaceGP(content);
                    CMSEntities cms = new CMSEntities();
                    News news = new News();
                    news.Source = source;
                    news.step = 0;
                    news.content = content;
                    news.ctime = DateTime.Now;
                    news.title = title;
                    news.keyword = title;
                    News_Type newsType = new News_Type();
                    newsType.NewsTypeId = "0501";
                    newsType.ctime = DateTime.Now;
                    news.News_Type.Add(newsType);
                    cms.News.AddObject(news);
                    cms.SaveChanges();
                    NewsCatch nCatch = new NewsCatch();
                    nCatch.url = url;
                    nCatch.NewsID = news.id;
                    nCatch.CTime = DateTime.Now;
                    cms.NewsCatch.AddObject(nCatch);
                    cms.SaveChanges();
                    new NewsImportBLL().ImportBk(news);
                }
            }
            catch
            {
            }
        }
    }

    bool IsCheck(string url)
    {
        CMSEntities cms = new CMSEntities();
        return cms.NewsCatch.Any(x => x.url == url);
    }
}